<template>
  <div>
    <meta name="format-detection" content="telephone=yes" />

    <div>
      <van-nav-bar title="增加收货地址"
                   left-text="返回"
                   @click-left="onClickLeft"
                   left-arrow
      />
    </div>

    <div class="container">
      <div>

        <van-address-edit
          :area-list="areaList"
          show-postal
          show-delete
          show-set-default
          show-search-result
          :address-info="addressInfo"
          :show-postal="false"
          :search-result="searchResult"
          :area-columns-placeholder="['请选择', '请选择', '请选择']"
          @save="saveAgencyAddress"
          @delete="onDelete"
          @change-detail="onChangeDetail"
        />

      </div>

    </div>

  </div>
</template>

<script>
  import {queryShareInfo,addShareLy,queryShareLy,addShareStoreInfo ,addLyhfinfo,queryAgencyinfo,queryProductSku ,saveAgencyAddress,queryAddressById ,updateAgencyAddress } from 'api/api'
  import {Toast} from 'vant'
  import AreaList from '../../../static/area'
  import eventBus from '../../utils/eventbus'



  export default {
    name: 'addAddress',
    data() {
      return {
        areaList:AreaList,
        agencyId:'',
        addressId:'',
        addressInfo:{},
        productId:'',
        skuId:'',
        selectedNum:'',
        price:'',
        totalPrice :'',
      }
    },
    created() {

      let agencyId = this.$route.query.agencyId
      if(!agencyId){
        agencyId = this.$store.getters.agencyId
      }
      this.agencyId =  agencyId
      console.log('agencyid:' , agencyId )

      let addressId = this.$route.query.addressId
      this.addressId = addressId
      if(addressId){
        this.queryAgenyAddress()
      }

      this.productId =  this.$route.query.productId
      this.skuId =  this.$route.query.skuId
      this.selectedNum = this.$route.query.selectedNum
      this.price = this.$route.query.price
      this.totalPrice = this.price * this.selectedNum * 100


    },
    methods: {

      onClickLeft(){
        let data = {}
        data.productId = this.productId
        data.price = this.price
        data.totalPrice = this.totalPrice
        data.selectedNum = this.selectedNum
        data.skuId = this.skuId
        eventBus.$emit('addressInfo',data);
        this.$router.go(-1) // 返回上一层
      },

      onSave(data){

        console.log('地址：',data )
      },

      saveAgencyAddress(data ){
        console.log('经销商ID:',this.agencyId)
        let param = data
        data.agencyId = this.agencyId

        if(data.isDefault){
          data.mrflag = '1'
        }else{
          data.mrflag = '0'
        }

        if(data.addressId){
          updateAgencyAddress(param).then(res =>{
            if(res.code == 200 ){
              Toast.success('保存成功')
              res.data.productId = this.productId
              res.data.price = this.price
              res.data.totalPrice = this.totalPrice
              res.data.selectedNum = this.selectedNum
              res.data.skuId = this.skuId
              eventBus.$emit('addressInfo',res.data);
              //调用router回退页面
              this.$router.go(-1) // 返回上一层
            }
          })
        }else{
          saveAgencyAddress(param).then(res =>{
            if(res.code == 200 ){
              Toast.success('保存成功')
              res.data.productId = this.productId
              res.data.price = this.price
              res.data.totalPrice = this.totalPrice
              res.data.selectedNum = this.selectedNum
              res.data.skuId = this.skuId

              eventBus.$emit('addressInfo',res.data);
              //调用router回退页面
              this.$router.go(-1);
            }
          })
        }
      },

      queryAgenyAddress(){
        let param = {}
        param.addressId  = this.addressId
        queryAddressById(param).then(res =>{
          if(res.code == 200 ){
              this.addressInfo = res.data
              if(res.data ){
                if(res.data.mrflag == '1'){
                  this.addressInfo.isDefault = true
                }
              }
          }
        })
      }
    },

  }
</script>

<style scoped>

  .container{
    background-color: white;
  }

  .liuyan{
    background-color: white;
    margin-top: 20px;
    padding-bottom: 60px;
  }
  .columnr{
    display: flex;
    flex-direction: column;
    justify-content: flex-start;
    align-items: flex-start;
  }

  .rownr{
    display: flex;
    flex-direction: row;
    justify-content: flex-start;
    align-items: flex-start;
    width: 100%;
  }

  .name{
    padding-top: 10px;
    padding-left: 10px;
    font-size: 20px;
    color: #466BAC;
  }

  .img{
    padding-top: 10px;
    padding-left: 10px;
  }

  .content{
    font-size: 16px;
    padding:10px;
    text-indent: 2em;
  }

  .viewimg{
    width: 90%;
    padding-top: 10px;
    padding-left: 5%;
    padding-right: 4%;
  }

  .title_bg{
    display: flex;
    flex-direction: row;
    justify-content: flex-start;
    align-items: center;
    padding:10px
  }
  .title_sx{
    height: 20px;
    width: 3px;
    background-color: #f50;
  }
  .title_name{
    color:#333333;
    font-size: 20px;
    margin-left: 5px;
    font-weight: bold;
  }

  .hfcontent{
    padding-top: 10px;
    padding-left: 60px;
    font-size: 16px;
  }

  .lyban{
    width: 99%;
    position: fixed;
    bottom: 0;
    height: 50px;
    background-color: #ffffff;
    border-top: 1px solid #B2B2B2 ;
  }

  .lytextcss{
    margin-top:8px;
    margin-left:10px;
    border-radius:15px;
    border: 1px solid #B2B2B2;
    width: 90%;
    height: 30px;
  }

  .textareacss{
    margin-left:10px;
    margin-right:10px;
    margin-top: 10px;
    margin-bottom: 20px;
    border: 1px solid #B2B2B2;
  }

  .hfcss{
    padding-left: 60px;
    padding-top: 10px;
    font-size: 16px;
  }

</style>
